#!/bin/sh
#
# inpanel - this script starts and stops the inpanel daemon
#
# chkconfig:   - 85 15
# description: InPanel is a web-based linux VPS management tools
# processname: inpanel
# pidfile:     /var/run/inpanel.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/local/inpanel/server.py"
pidfile="/var/run/inpanel.pid"

lockfile=/var/lock/subsys/inpanel

start() {
    [ -x $inpanel ] || exit 5
    echo -n $"Starting InPanel: "
    daemon --pidfile="$pidfile" "$exec &"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping InPanel: "
    killproc -p "$pidfile" $exec
    #killproc $prog -TERM
    retval=$?
    if [ $retval -eq 0 ]; then
        if [ "$CONSOLETYPE" != "serial" ]; then
           echo -en "\\033[16G"
        fi
        while rh_status_q
        do
            sleep 1
            echo -n $"."
        done
        rm -f $lockfile
    fi
    echo
    return $retval
}

restart() {
    stop
    start
}

uninstall() {
    echo -n $"Deleting InPanel: "
    stop
    rm -f /etc/init.d/inpanel
    echo -n $"InPanel Daemon Deleted "
    mkdir /root/inpanel_backup_data
    cp /usr/local/inpanel/data /root/inpanel_backup_data
    echo -n $"Backup InPanel Data to /root/inpanel_backup_data"
    rm -rf /usr/local/inpanel
    echo -n $"InPanel Removed !"
}

rh_status() {
    status -p "$pidfile" $exec
    #status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status|status_q)
        rh_$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac
